'use strict';

const { Controller } = require('egg');

class OutgoingInvoiceController extends Controller {
  /**
   * 获取销项发票列表
   */
  async index() {
    const { ctx } = this;
    
    try {
      const query = ctx.query;
      const organizationId = ctx.state.user.organizationId;
      
      const result = await ctx.service.outgoingInvoiceService.getOutgoingInvoices(query, organizationId);
      
      // 使用统一的分页响应格式
      ctx.page(result.items, result.total, result.page, result.pageSize);
    } catch (error) {
      ctx.logger.error('[OutgoingInvoiceController] 获取发票列表失败:', error);
      ctx.error(error.message || '获取发票列表失败', error.status || 500);
    }
  }
  
  /**
   * 获取销项发票详情
   */
  async show() {
    const { ctx } = this;
    
    try {
      const { id } = ctx.params;
      const organizationId = ctx.state.user.organizationId;
      
      const invoice = await ctx.service.outgoingInvoiceService.getOutgoingInvoiceById(id, organizationId);
      
      ctx.body = {
        code: 0,
        message: 'success',
        data: invoice
      };
    } catch (error) {
      ctx.logger.error('[OutgoingInvoiceController] 获取发票详情失败:', error);
      ctx.body = {
        code: error.status || 500,
        message: error.message || '获取发票详情失败'
      };
    }
  }
  
  /**
   * 创建销项发票
   */
  async create() {
    const { ctx } = this;
    
    try {
      const invoiceData = ctx.request.body;
      const organizationId = ctx.state.user.organizationId;
      
      const invoice = await ctx.service.outgoingInvoiceService.createOutgoingInvoice(invoiceData, organizationId);
      
      ctx.body = {
        code: 0,
        message: '创建成功',
        data: invoice
      };
    } catch (error) {
      ctx.logger.error('[OutgoingInvoiceController] 创建发票失败:', error);
      ctx.body = {
        code: error.status || 500,
        message: error.message || '创建发票失败'
      };
    }
  }
  
  /**
   * 更新销项发票
   */
  async update() {
    const { ctx } = this;
    
    try {
      const { id } = ctx.params;
      const invoiceData = ctx.request.body;
      const organizationId = ctx.state.user.organizationId;
      
      const invoice = await ctx.service.outgoingInvoiceService.updateOutgoingInvoice(id, invoiceData, organizationId);
      
      ctx.body = {
        code: 0,
        message: '更新成功',
        data: invoice
      };
    } catch (error) {
      ctx.logger.error('[OutgoingInvoiceController] 更新发票失败:', error);
      ctx.body = {
        code: error.status || 500,
        message: error.message || '更新发票失败'
      };
    }
  }
  
  /**
   * 删除销项发票
   */
  async destroy() {
    const { ctx } = this;
    
    try {
      const { id } = ctx.params;
      const organizationId = ctx.state.user.organizationId;
      
      await ctx.service.outgoingInvoiceService.deleteOutgoingInvoice(id, organizationId);
      
      ctx.body = {
        code: 0,
        message: '删除成功'
      };
    } catch (error) {
      ctx.logger.error('[OutgoingInvoiceController] 删除发票失败:', error);
      ctx.body = {
        code: error.status || 500,
        message: error.message || '删除发票失败'
      };
    }
  }
}

module.exports = OutgoingInvoiceController;